package com.home.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.home.school.config.exception.AppException;
import com.home.school.vo.UserListVO;
import com.home.school.vo.UserModifyPwd;
import com.home.school.vo.UserModifyVO;
import com.home.school.vo.UserVO;
import com.home.school.model.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.home.school.mapper.TUserMapper;
import com.home.school.bean.TUser;
import com.home.school.service.TUserService;

import java.util.Objects;

@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements TUserService {

    @Override
    public R login(UserVO userVO) {
        String userName = userVO.getUserName();
        TUser tUser = selectByName(userName);
        Integer type = userVO.getType();
        if(Objects.isNull(tUser)){
            return R.error("用户不存在");
        }
        Integer userType = tUser.getType();
        if(!type.equals(userType)){
            return R.error("用户类型错误");
        }
        String password = tUser.getPassword();
        if(userVO.getPassword().equalsIgnoreCase(password)){
            return R.ok().message("登录成功").data(tUser);
        }
        return R.error("密码错误");
    }

    @Override
    public R modify(UserModifyVO userVO) {
        TUser tUser = baseMapper.selectById(userVO.getId());
        if(Objects.isNull(tUser)){
            throw new AppException("用户不存在");
        }
        TUser updateUser = userVO.transferToUser();
        baseMapper.updateById(updateUser);
        return R.ok();
    }

    @Override
    public R pageList(UserListVO userListVO) {
        Page<TUser> tUserPage = new Page<>(userListVO.getPageNum(), userListVO.getPageSize());
        IPage<TUser> tUserIPage = baseMapper.selectPage(tUserPage, queryWrapper(userListVO));
        return R.ok().data(tUserIPage);
    }

    @Override
    public void resetPwd(UserModifyPwd userVO) {
        String id = userVO.getId();
        TUser user = baseMapper.selectById(id);
        if(Objects.isNull(user)){
            throw new AppException("用户不存在");
        }
        if(!user.getPassword().equalsIgnoreCase(userVO.getOldPwd())){
            throw new AppException("旧密码错误");
        }
        user.setPassword(userVO.getPassword());
        baseMapper.updateById(user);
    }


    public QueryWrapper<TUser> queryWrapper(UserListVO tUser){
        QueryWrapper<TUser> tUserQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(tUser.getUserName())){
            tUserQueryWrapper.eq("username", tUser.getUserName());
        }
        if(StringUtils.isNotEmpty(tUser.getType())){
            tUserQueryWrapper.eq("type", tUser.getType());
        }
        return tUserQueryWrapper;
    }

        /**
     * 用户名查询
     * @param name
     * @return
     */
    public TUser selectByName(String name) {
        QueryWrapper<TUser> tUserQueryWrapper = new QueryWrapper<>();
        tUserQueryWrapper.eq("username",name);
        return baseMapper.selectOne(tUserQueryWrapper);
    }
}
